﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using System.Web;
using FantasyBlog.DAL;

namespace FantasyBlog.MemeberShip
{
    public class BlogMemberShipProvider:MembershipProvider
    {
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            var user = (from u in ModelContainer.UserSet
                        where u.username == username && u.password ==oldPassword
                        select u).FirstOrDefault();
            if(user == null)
                return false;
            user.password = newPassword;
            ModelContainer.SaveChanges();
            return true;
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            status = MembershipCreateStatus.Success;
            try
            {
                var user = new User();
                user.username = username;
                user.password = password;
                user.email = email;
                user.displayname = username;
                user.registered = DateTime.Now;
                user.status = "test";
                ModelContainer.UserSet.Add(user);
                ModelContainer.SaveChanges();
            }
            catch
            {
                status = MembershipCreateStatus.DuplicateUserName;
            }
            
            return new MembershipUser(Membership.Provider.Name, username, null, email, null, null, true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);

        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            var user = (from u in ModelContainer.UserSet
                        where u.username == username
                        select u).FirstOrDefault();
            if (user == null)
                return false;
            ModelContainer.UserSet.Remove(user);
            ModelContainer.SaveChanges();
            return true;
        }

        public override bool EnablePasswordReset
        {
            get{return false;}
        }

        public override bool EnablePasswordRetrieval
        {
            get {return false;}
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            totalRecords = 0;
            return null;
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {

            var users = (from user in ModelContainer.UserSet
                         orderby user.username
                         select user).Skip(pageIndex * pageSize).Take(pageSize);
            var c = new MembershipUserCollection();
            foreach (var user in users)
            {
                c.Add(new MembershipUser(
                            Membership.Provider.Name,
                            user.username,
                            null,
                            user.email,
                            null,
                            null,
                            true,
                            false,
                            user.registered,
                            DateTime.MinValue,
                            DateTime.MinValue,
                            DateTime.MinValue,
                            DateTime.MinValue));
            }
            totalRecords = c.Count;
            return c;
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
             return null;
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            return (from u in ModelContainer.UserSet
                    where u.email == email
                    select u.username).FirstOrDefault();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return maxInvalidPasswordAttempts; }
        }
        int maxInvalidPasswordAttempts = 3;

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return minRequiredNonAlphanumericCharacters; }
        }
        int minRequiredNonAlphanumericCharacters = 0;

        public override int MinRequiredPasswordLength
        {
            get { return minRequiredNonAlphanumericCharacters; }
        }
        int minRequiredPasswordLength = 6;

        public override int PasswordAttemptWindow
        {
            get { return passwordAttemptWindow; }
        }
        int passwordAttemptWindow = 3;

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return false; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return false; }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            var user = (from u in ModelContainer.UserSet
                        where u.username == username && u.password == password
                        select u).FirstOrDefault();
            return (user != null);
        }

        BlogModelContainer ModelContainer = new BlogModelContainer();   
    }
}